package com.tao.plan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tao.plan.common.BaseThreadLocal;
import com.tao.plan.domain.pojo.PCommonPlan;
import com.tao.plan.domain.vo.CommonPlanCountVo;
import com.tao.plan.mapper.PCommonPlanMapper;
import com.tao.plan.service.PCommonPlanService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PCommonPlanServiceImpl extends ServiceImpl<PCommonPlanMapper, PCommonPlan> implements PCommonPlanService {
	@Override
	public List<PCommonPlan> listByTypeAndStatus(Integer type,Integer status) {
		LambdaQueryWrapper<PCommonPlan> lqw = new LambdaQueryWrapper<>();
		lqw.eq(PCommonPlan::getType,type).eq(PCommonPlan::getStatus,status).eq(PCommonPlan::getUid, BaseThreadLocal.getUid()).orderByDesc(PCommonPlan::getPriority);
		return this.baseMapper.selectList(lqw);
	}

	@Override
	public CommonPlanCountVo countDoingTask() {
		LambdaQueryWrapper<PCommonPlan> lqw = new LambdaQueryWrapper<>();
		lqw.eq(PCommonPlan::getUid,BaseThreadLocal.getUid()).eq(PCommonPlan::getStatus,0);
		List<PCommonPlan> pCommonPlans = this.baseMapper.selectList(lqw);
		Map<Integer, List<PCommonPlan>> collect = pCommonPlans.stream().collect(Collectors.groupingBy(PCommonPlan::getType));
		CommonPlanCountVo commonPlanCountVo = new CommonPlanCountVo();
		commonPlanCountVo.setStudyCount(collect.get(0).size());
		commonPlanCountVo.setWorkCount(collect.get(1).size());
		commonPlanCountVo.setLifeCount(collect.get(2).size());
		return commonPlanCountVo;
	}

	@Override
	public List<PCommonPlan> listByStatus(Integer status, Long userId) {
		LambdaQueryWrapper<PCommonPlan> lqw = new LambdaQueryWrapper<>();
		lqw.eq(PCommonPlan::getStatus,status).eq(PCommonPlan::getUid, userId).orderByDesc(PCommonPlan::getPriority);
		return this.baseMapper.selectList(lqw);

	}
}
